package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.dto.DownLoad;
import com.project.entity.Project;
import com.project.mapper.DownLoadMapper;
import com.project.service.AccessoryService;
import com.project.entity.Accessory;
import com.project.mapper.AccessoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccessoryServiceImpl extends ServiceImpl<AccessoryMapper, Accessory> implements AccessoryService {

    @Autowired
    private DownLoadMapper downLoadMapper;

    public Page download(int page, int pageSize, Date begin, Date end, Long[] projectId){
        //分页构造器
        Page<Accessory>accessoryPage=new Page<>(page,pageSize);
        Page<DownLoad> pageInfo=new Page<>();

        //条件构造器
        LambdaQueryWrapper<Accessory> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Accessory::getUpTime);
        lambdaQueryWrapper.between(begin!=null|end!=null,Accessory::getUpTime,begin,end);
        lambdaQueryWrapper.in(Accessory::getProjectId,projectId);
        this.page(accessoryPage,lambdaQueryWrapper);



        //对象拷贝
        BeanUtils.copyProperties(accessoryPage,pageInfo,"records");
        List<Accessory> records = accessoryPage.getRecords();

        List<DownLoad> list=records.stream().map((item)->{
            DownLoad downLoad=new DownLoad();
            BeanUtils.copyProperties(item,downLoad);

            //上传者姓名
            String upName = downLoadMapper.getName(item.getProjectId(), item.getName(),item.getType());
            downLoad.setUpName(upName);

            //flag:1  可以下载
            downLoad.setFlag(1);
            return downLoad;
        }).collect(Collectors.toList());

        pageInfo.setRecords(list);

        return pageInfo;




    }
}
